package com.zhq.blog.core.domain.request;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import cz.mallat.uasparser.OnlineUpdater;
import cz.mallat.uasparser.UASparser;
import cz.mallat.uasparser.UserAgentInfo;

/**
 * @title:RequestContext.java
 * @package com.zhq.blog.core.domain.request
 * @author 张恒强
 * @date 2015年1月5日 上午9:52:24
 * @version V1.0
 */
public class RequestContext {
	
	public static final String UA_FLAG = "User-Agent";
	public static final String REFERER_FLAG = "User-Agent";
	public static final String LANGUAGE_FLAG ="Accept-Language";
	
	public static UASparser parser = new UASparser();
	static{
		parser = new UASparser();
		new OnlineUpdater(parser);
	}
	private String requestIP;
	private String ETAG;

	private String action;
	private String url;
	private String referer;
	private String browser;
	private String os;
	private String userAgent;
	private String language;
	private String method;
	private String directIP;
	private String sourceIP;
	private String nation;
	private String state;
	private String address;

	/**
	 * 得到用戶的原始ip
	 * 适用于CDN和ngnix转发的情形
	 * 但是用户可能借http头伪造ip
	 * @param request
	 * @return 用戶的原始ip
	 */
	public String getSourceIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public void init(HttpServletRequest request) {
		
		try {
			UserAgentInfo info = parser.parse("Moz");

			System.out.println(info.getBrowserVersionInfo());
			System.out.println(info);
			System.out.println(info.getOsName());
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	public String getRequestIP() {
		return requestIP;
	}

	public void setRequestIP(String requestIP) {
		this.requestIP = requestIP;
	}

	public String getETAG() {
		return ETAG;
	}

	public void setETAG(String eTAG) {
		ETAG = eTAG;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getReferer() {
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
	}

	public String getBrowser() {
		return browser;
	}

	public void setBrowser(String browser) {
		this.browser = browser;
	}

	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getDirectIP() {
		return directIP;
	}

	public void setDirectIP(String directIP) {
		this.directIP = directIP;
	}

	public String getSourceIP() {
		return sourceIP;
	}

	public void setSourceIP(String sourceIP) {
		this.sourceIP = sourceIP;
	}

	public String getNation() {
		return nation;
	}

	public void setNation(String nation) {
		this.nation = nation;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	

}
